package com.dgut.ibeacon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.ibeacon.entity.AjaxResult;
import com.dgut.ibeacon.entity.SearchLog;
import com.dgut.ibeacon.entity.Stock;
import com.dgut.ibeacon.service.ISearchLogService;
import com.dgut.ibeacon.service.IStockService;
import com.dgut.ibeacon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuec
 * @since 2020-10-14
 */
@Controller
@RequestMapping("//stock")
public class StockController {

    @Autowired
    IStockService stockService;
    @Autowired
    ISearchLogService searchLogService;

    @GetMapping("/addBooks")
    public String addBooks(){
        return "addBooks";
    }

    @GetMapping("/checkBooks")
    public String checkBooks(){
        return "checkBooks";
    }

    /**
     * 普通用户没有添加图书的权限
     * @param stock
     * @return
     */
    @RequiresRoles(value = {"super_admin","admin"})
    @ResponseBody
    @RequestMapping("/createBook")
    public AjaxResult createBook(Stock stock){
        if (stockService.save(stock)==true){
            return AjaxResult.success("图书名为"+stock.getBookName()+"创建成功");
        }else {
            return AjaxResult.error("图书创建失败，存在信息不完整");
        }
    }

    /**
     * 普通用户没有修改图书信息的权限
     * @param stock
     * @return
     */
    @RequiresRoles(value = {"super_admin","admin"})
    @ResponseBody
    @RequestMapping("/alterBook")
    public AjaxResult alterBook(Stock stock){
        if (stockService.updateById(stock)){
            return AjaxResult.success("图书名为"+stock.getBookName()+"修改信息成功");
        }else {
            return AjaxResult.error("图书修改信息失败，存在信息不完整");
        }
    }

    /**
     * 普通用户没有删除图书信息的权限
     * @param bookId
     * @return
     */
    @RequiresRoles(value = {"super_admin","admin"})
    @ResponseBody
    @RequestMapping("/deleteBook")
    public AjaxResult deleteBook(long bookId){
        if (stockService.removeById(bookId)){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }


    /**
     * 根据输入的书名进行模糊查询
     * @param inputName
     * @param index
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/listByName")
    public AjaxResult listByName(@RequestParam(value = "inputName") String inputName,
                           @RequestParam(value = "index",defaultValue = "0") int index,
                           @RequestParam(value = "size",defaultValue = "15") int size){
        IPage<Stock> page=new Page<>(index,size);
        page=stockService.page(page,new QueryWrapper<Stock>().like("book_name",inputName));
        return AjaxResult.success(page);
    }

    /**
     * 根据书架号查询图书（按照入库时间倒叙）
     * @param bookshelfId
     * @param index
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/listByBookshelfId")
    public AjaxResult listByBookshelfId(@RequestParam(value = "bookshelfId") String bookshelfId,
                                 @RequestParam(value = "index",defaultValue = "0") int index,
                                 @RequestParam(value = "size",defaultValue = "15") int size){
        IPage<Stock> page=new Page<>(index,size);
        page=stockService.page(page,new QueryWrapper<Stock>().eq("bookshelf_id",bookshelfId).orderByDesc("gmt_create"));
        return AjaxResult.success(page);
    }

    /**
     * 根据客户端点击，返回书号查询结果，并记录用户的查询
     * @param bookId
     * @return
     */
    public AjaxResult clickBook(long bookId){
        Stock currBook=stockService.getById(bookId);
        if (currBook!=null){
            SearchLog searchLog=new SearchLog();
            searchLog.setBookId(currBook.getBookId());
            searchLog.setUserId(ShiroUtils.getUserId());
            searchLogService.save(searchLog);
            return AjaxResult.success(currBook);
        }else {
            return AjaxResult.error("未查询到该书");
        }
    }





}
